from net import *
from database import *
from rna_core import CAnn

def main():

    net = CNet(True,  [],  3,  [2, 2, 1])
    ann = CAnn(net)
    db = CDatabase('temporal6')
    
    """
    #To load a fix synapses
    fixSynapses = [[[1,  2],  [-1, 0],  [0, 1]],  [[1,  0.5], [0.5,  0.5],  [0.5,  0.5]],  [[1],  [-0.5],  [-0.5]]]
    net = CNet(False,  fixSynapses)
    ann = CAnn(net)
    ann.loadPlugins()
    
    net.printSynapses()
    ann.net.propagate([1, 0, 0])
    ann.algorithm.backpropagation(ann, [0])
    ann.algorithm.adjustSynapsis(ann)
    net.printSynapses()    

    """
    #create tables
    db.createTables()

    
    #Load inputs/expectedOutputs into database.
    #This step is just for test
    inputs = [[1, 0, 0], 
              [1, 0, 1], 
              [1, 1, 0], 
              [1, 1, 1]]
    eOutputs = [[1],[0],[0],[1]]
    for i in range(0, len(inputs)):
        db.saveIO(inputs[i], eOutputs[i])
    
    #Extract values from database
    inputsDb = db.getValues('inputs')
    outputsDb = db.getValues('eOutputs')
    ann.loadPlugins()
    
    for iteration in range(0, 9000):
        print "."
        #id = 0
        for x in inputsDb.keys():
            ann.net.propagate(inputsDb[x])
            ann.algorithm.backpropagation(ann, outputsDb[x])
            ann.algorithm.adjustSynapsis(ann)
            if ((iteration%100) == 0):
                db.saveOutputs(x, iteration, ann.net.layers['outputLayer'].values)
    ann.net.printSynapses()
    db.saveSynapses(ann.net.synapses)
    
    #test current trained network
    #temporal, will be deleted
    ann.net.propagate([1, 0, 0])
    for value in ann.net.layers["outputLayer"].values:
        print "valor obtenido: "
        print value
    ann.net.propagate([1, 0, 1])
    for value in ann.net.layers["outputLayer"].values:
        print "valor obtenido: "
        print value
    ann.net.propagate([1, 1, 0])
    for value in ann.net.layers["outputLayer"].values:
        print "valor obtenido: "
        print value
    ann.net.propagate([1, 1, 1])
    for value in ann.net.layers["outputLayer"].values:
        print "valor obtenido: "
        print value
    
if __name__ == "__main__":
    main()
    
    
    

